package com.yystar.support.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import com.yystar.support.util.CacheUtility;
import com.yystar.support.util.SystemProperties;
import com.yystar.support.util.WTextUtility;

/***
 * 处理Spring拦截后处理的缓存
 * @author liuzhifei
 *
 */
public class NoCacheFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletresponse, FilterChain chain) throws IOException, ServletException {
        
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        
        final boolean isDevelopmentModel = WTextUtility.parseBooleanString(
                SystemProperties.get("is.development.model"));
        
        final boolean containsCache = CacheUtility.containsCache(request.getRequestURI());

        chain.doFilter(new HttpServletRequestWrapper(request){
            
            @Override
            public long getDateHeader(String name) {
               
                // 如果是开发状态 则取消 If-Modified-Since
                if((isDevelopmentModel && "If-Modified-Since".equalsIgnoreCase(name)) ||
                       (!containsCache && "If-Modified-Since".equalsIgnoreCase(name))){
                    return -1;
                }
                
                return super.getDateHeader(name);
            }
            
            @Override
            public String getHeader(String name) {
                
                // 如果是开发状态 则取消 If-Modified-Since
                if((isDevelopmentModel && "If-Modified-Since".equalsIgnoreCase(name)) ||
                        (!containsCache && "If-Modified-Since".equalsIgnoreCase(name))){
                    return null;
                }
                
                return super.getHeader(name);
            }
            
            }, new HttpServletResponseWrapper((HttpServletResponse) servletresponse){
            
            @Override
            public void setDateHeader(String name, long date) {
                
                // 如果是开发状态 则取消Last-Modified缓存
                if((isDevelopmentModel && "Last-Modified".equalsIgnoreCase(name)) ||
                        (!containsCache && "Last-Modified".equalsIgnoreCase(name))){
                    return;
                }
                
                // TODO Auto-generated method stub
                super.setDateHeader(name, date);
            }
            
            @Override
            public void setHeader(String name, String value) {
                
                if(!containsCache && "Last-Modified".equalsIgnoreCase(name)){
                    return;
                }
                
                 // 如果是开发状态 则取消 Last-Modified缓存和Etag
                 if(isDevelopmentModel && ("Last-Modified".equalsIgnoreCase(name) || "Etag".equalsIgnoreCase(name))){
                     return;
                 }
                 
                 super.setHeader(name, value);
            } 
        });
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}
